package com.werdior.weixinbackend.www;

import com.github.sd4324530.fastweixin.api.UserAPI;
import com.github.sd4324530.fastweixin.api.config.ApiConfig;
import com.github.sd4324530.fastweixin.api.response.GetUserInfoResponse;
import com.github.sd4324530.fastweixin.api.response.GetUsersResponse;
import com.github.sd4324530.fastweixin.message.BaseMsg;
import com.github.sd4324530.fastweixin.message.ImageMsg;
import com.github.sd4324530.fastweixin.message.TextMsg;
import com.github.sd4324530.fastweixin.message.VideoMsg;
import com.github.sd4324530.fastweixin.message.req.*;
import com.github.sd4324530.fastweixin.servlet.WeixinControllerSupport;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by weirdor on 2017/12/26.
 */
@RestController
@RequestMapping("/wx")
public class wxController extends WeixinControllerSupport {

    private static final Logger log = LoggerFactory.getLogger(wxController.class);

    private static final String TOKEN = "weirdor";

    @Autowired
    private ApiConfig apiConfig;


    /**
     * @param request
     * @param response
     * @throws Exception
     * @Title accessWechat
     * @Description 用于用户在微信端连接服务器
     */
    @RequestMapping(value = "/accessWechat", method = RequestMethod.GET)
    public void accessWechat(HttpServletRequest request, HttpServletResponse response) throws Exception {

        super.bindServer(request, response);
    }

    /**
     * 微信发送消息
     * */
    @RequestMapping(value = "/accessWechat", method = RequestMethod.POST)
    public String Message(HttpServletRequest request, HttpServletResponse response) throws Exception {
        return super.processRequest(request);
    }


    //重写父类方法，处理对应的微信消息
    @Override
    protected BaseMsg handleTextMsg(TextReqMsg msg) {
        String content = msg.getContent();
        log.debug("用户发送到服务器的内容:{}", content);
        List<String> list=new ArrayList<>();
        if (content.equals("关注列表")){
            UserAPI userAPI = new UserAPI(apiConfig);
            GetUsersResponse users = userAPI.getUsers(null);
            log.debug("user count:{}", users.getCount());
            log.debug("user total:{}", users.getTotal());
            String[] openids = users.getData().getOpenid();

            for (String id : openids) {
                log.debug("id:{}", id);
                GetUserInfoResponse userInfo = userAPI.getUserInfo(id);
                log.info(userInfo.toJsonString());
                list.add(userInfo.toJsonString());
        }
            return new TextMsg(list.toString());
        }
        return new TextMsg("用户发送到服务器的内容:"+content);
    }

    @Override
    protected BaseMsg handleLinkMsg(LinkReqMsg msg) {
        return super.handleLinkMsg(msg);
    }

    @Override
    protected BaseMsg handleVoiceMsg(VoiceReqMsg msg) {
        return super.handleVoiceMsg(msg);
    }

    @Override
    protected BaseMsg handleVideoMsg(VideoReqMsg msg) {
        return new VideoMsg(msg.getMediaId());
    }

    /**
     * 关注触发
     */

    @Override
    protected BaseMsg handleSubscribe(BaseEvent event) {
        UserAPI userAPI = new UserAPI(apiConfig);
        GetUserInfoResponse getUserInfoResponse = userAPI.getUserInfo(event.getFromUserName());
        log.info("获取用户信息 {}", getUserInfoResponse.toJsonString());
        return new TextMsg(getUserInfoResponse.toJsonString());
    }


    /**
     * 取消关注触发
     */
    @Override
    protected BaseMsg handleUnsubscribe(BaseEvent event) {
        log.info("有用户取消关注微信公众号");
        return super.handleUnsubscribe(event);
    }


    @Override
    protected BaseMsg handleImageMsg(ImageReqMsg msg) {
        return new ImageMsg( msg.getMediaId());
    }


    @Override
    protected String getToken() {
        return TOKEN;
    }

}
